package elephant.rpc.server.session;

import java.util.Set;
import java.util.TreeSet;

import elephant.rpc.server.core.RPCChannel;
import io.netty.util.AttributeKey;

/**
 * 
 * @author skydu
 *
 */
public class RPCSession {

	public static final AttributeKey<RPCSession> SESSION_KEY=
			AttributeKey.valueOf("RPCSession");
	
	public int id;
	
	public String clientUuid;
	
	public String serverCluster;
	
	public String remoteHost;
	
	public int remotePort;
	
	public Set<String> topics;
	
	public RPCChannel channel;
	
	public long createTime;

	public long lastAccessTime;
	
	public RPCSession(){
		topics=new TreeSet<>();
		createTime=System.currentTimeMillis();
		lastAccessTime=System.currentTimeMillis();
	}
	//
	//
	public void write(Object message){
		channel.write(message);
	}
	//
	public boolean isConnected() {
		if(channel==null){
			return false;
		}
		return channel.isConnected();
	}
	//
	public void disconnect(){
		if(channel==null){
			return;
		}
		channel.disconnect();
	}
	//
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "RPCSession [id=" + id + ", clientUuid=" + clientUuid + ", serverCluster=" + serverCluster
				+ ", remoteHost=" + remoteHost + ", remotePort=" + remotePort + ", topics=" + topics + ", channel="
				+ channel + ", createTime=" + createTime + "]";
	}
	
}
